package wtc.stu.dao;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StuDaoTeacher {
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;

    public StuDaoTeacher()
    {
        dbc = new DBConnection();

    }
    // 全查询教师信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from teacherinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                StuEntity teacher = new StuEntity();
                teacher.setTid(rs.getInt("tid"));
                teacher.setTname(rs.getString("tname"));
                teacher.setXid(rs.getInt("xid"));
                teacher.setTremark(rs.getString("tremark"));
                ar.add(teacher);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }
    // 查询一个教师信息表对象
    public StuEntity getOneById(int tid)
    {
        StuEntity teacher = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from teacherinfo where tid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, tid);
            rs = pre.executeQuery();
            while (rs.next())
            {
                teacher.setTid(rs.getInt("tid"));
                teacher.setTname(rs.getString("tname"));
                teacher.setXid(rs.getInt("xid"));
                teacher.setTremark(rs.getString("tremark"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return teacher;
    }
    // 增加教师信息表对象
    public void addStu(StuEntity teacher)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into teacherinfo values(null,?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setString(1,teacher.getTname());
            pre.setInt(2,teacher.getXid());
            pre.setString(3, teacher.getTremark());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
    // 修改教师信息表对象
    public void updateStu(StuEntity teacher)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  teacherinfo  set tname=?, xid=?, tremark=? where tid=?";
            pre = conn.prepareStatement(sql);
            pre.setString(1, teacher.getTname());
            pre.setInt(2, teacher.getXid());
            pre.setString(3, teacher.getTremark());
            pre.setInt(4,teacher.getTid());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}
